package com.ruoyi.web.controller.system;

import java.math.BigDecimal;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.BigDecimalUtil;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.TGoods;
import com.ruoyi.system.domain.TGoodsCarts;
import com.ruoyi.system.domain.TOrdersItems;
import com.ruoyi.system.service.ITGoodsService;
import com.ruoyi.system.service.ITOrdersItemsService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.TOrders;
import com.ruoyi.system.service.ITOrdersService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 订单 Controller
 * 
 * @author ruoyi
 * @date 2022-04-09
 */
@RestController
@RequestMapping("/system/orders")
public class TOrdersController extends BaseController
{
    @Autowired
    private ITOrdersService tOrdersService;

    @Autowired
    private ITGoodsService itGoodsService;

    @Autowired
    private ITOrdersItemsService itOrdersItemsService;

    /**
     * 查询订单 列表
     */
    @GetMapping("/list")
    public TableDataInfo list(TOrders tOrders)
    {
        startPage();
        List<TOrders> list = tOrdersService.selectTOrdersList(tOrders);
        return getDataTable(list);
    }

    /**
     * 修改订单
     */
    @PutMapping
    public AjaxResult edit(@RequestBody TOrders tOrders)
    {
        if(tOrders.getStatus()==2){
            tOrders.setPaiedTime(DateUtils.getNowDate());
        }
        tOrders.setUpdatedTime(DateUtils.getNowDate());
        return toAjax(tOrdersService.updateTOrders(tOrders));
    }

    @PostMapping("/addOrder/{goodsId}")
    @Transactional
    public AjaxResult addOrder(@PathVariable String[] goodsId)
    {
        TOrders orders=new TOrders();
        orders.setOrderId(tOrdersService.getOrderId());
        orders.setCreatedTime(DateUtils.getNowDate());
        BigDecimal total=new BigDecimal(0.0);
        for(String id:goodsId){
            TGoods goods=itGoodsService.selectTGoodsByGoodsId(id);
            TOrdersItems ordersItems=new TOrdersItems();
            ordersItems.setOrderId(orders.getOrderId());
            ordersItems.setGoodsId(goods.getGoodsId());
            ordersItems.setCreatedTime(DateUtils.getNowDate());
            ordersItems.setGoodsName(goods.getName());
            ordersItems.setOrderType("1");
            ordersItems.setUnitPrice(BigDecimalUtil.mul(1.0,goods.getVipPrice().doubleValue()));
            itOrdersItemsService.insertTOrdersItems(ordersItems);
            total=BigDecimalUtil.add(total.doubleValue(),ordersItems.getUnitPrice().doubleValue());
        }
        orders.setOrderType(1);
        orders.setStatus(1);
        orders.setCreatedTime(DateUtils.getNowDate());
        orders.setUpdatedTime(DateUtils.getNowDate());
        orders.setTotalFeePaid(total);
        tOrdersService.insertTOrders(orders);
        return AjaxResult.success("操作成功",orders.getOrderId());
    }

}
